﻿using CommonServiceLocator;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using 阿耀超市智能化运营系统.Entity;
using 阿耀超市智能化运营系统.View;

namespace 阿耀超市智能化运营系统.ViewModel
{
    public class CustomerViewModel : ViewModelBase2
    {
        private CustomerProvider customerProvider = new CustomerProvider();

        private List<Customer> _customerList = new List<Customer>();
        public List<Customer> CustomerList
        {
            get { return _customerList; }
            set { _customerList = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 当前选中顾客的实例
        /// </summary>
        private Customer customer ;
        public Customer Customer
        {
            get { return customer; }
            set { customer = value; RaisePropertyChanged(); }
        }

        #region Commands

        public RelayCommand<UserControl> LoadedCommand
        {
            get
            {
                return new RelayCommand<UserControl>((view) =>
                {
                    CustomerList = customerProvider.GetAll();
                    Customer = null;
                });
            }
        }

        public RelayCommand<UserControl> OpenAddViewCommand
        {
            get
            {
                return new RelayCommand<UserControl>((view) =>
                {
                    AddCustomerView addCustomerView = new AddCustomerView();
                    if (addCustomerView.ShowDialog().Value == true)
                    {
                        CustomerList = customerProvider.GetAll();
                    }
                });
            }
        }

        public RelayCommand<UserControl> DeleteCommand
        {
            get
            {
                return new RelayCommand<UserControl>((view) =>
                {
                    if (Customer == null) return;
                    if (Dialog.Show())
                    {
                        var count = customerProvider.Delete(Customer);
                        if (count > 0)
                        {
                            MessageBox.Show("删除成功");
                        }
                        CustomerList = customerProvider.GetAll();
                    }
                });
            }
        }

        public RelayCommand<UserControl> SaveCommand
        {
            get
            {
                return new RelayCommand<UserControl>((view) =>
                {
                    int count = customerProvider.Save();
                    if (count > 0)
                    {
                        MessageBox.Show("保存成功");
                    }
                });
            }
        }

        public RelayCommand<UserControl> EditCommand
        {
            get
            {
                return new RelayCommand<UserControl>((view) =>
                {
                    if (Customer == null) return;
                    // 直接从当前IOC容器中拿ViewModel
                    // 而选中的SelectedItem="{Binding Customer}" 这种做法很强
                    var vm = ServiceLocator.Current.GetInstance<EditCustomerViewModel>();
                    vm.Customer = customer;
                    EditCustomerView editCustomerView = new EditCustomerView();
                    if (editCustomerView.ShowDialog().Value == true)
                    {
                        // 重新更新表格
                        CustomerList = customerProvider.GetAll();
                    }
                });
            }
        }


        #endregion
    }
}
